<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
    use SoftDeletes;

    const GENDER_UNKNOWN = 0;
    const GENDER_MALE = 1;

    // 移除批量赋值属性保护
    const GENDER_FEMALE = 2;
    const GENDER_LIST = [
        self::GENDER_UNKNOWN => '未知',
        self::GENDER_MALE => '男',
        self::GENDER_FEMALE => '女',
    ]; // 未知
    //身份 1=>学生族 2=>上班族 3=>宝妈 4=>待业 5=>自由职业
    const IDENTITY_STUDENT = 1; // 学生族
    const IDENTITY_EMPLOYEE = 2; // 上班族
    const IDENTITY_MOM = 3; //宝妈 
    const IDENTITY_UNEMPLOYED = 4; // 待业
    const IDENTITY_FREELANCER = 5; // 自由职业
    const IDENTITY_UNKNO = 0; // 未知
    const IDENTITY_LIST = [
        self::IDENTITY_STUDENT => '学生族',
        self::IDENTITY_EMPLOYEE => '上班族',
        self::IDENTITY_MOM => '宝妈',
        self::IDENTITY_UNEMPLOYED => '待业',
        self::IDENTITY_FREELANCER => '自由职业',
        self::IDENTITY_UNKNO => '未知',
    ];
    
    // const IDENTITY_STUDENT = 1; // 男
    // const IDENTITY_EMPLOYEE = 2; // 女
    // const IDENTITY_MOM = 0; //未知
    // const IDENTITY_UNEMPLOYED = 4; // 学生族
    // const IDENTITY_FREELANCER = 5; // 上班族
    // const IDENTITY_LIST = [
    //     self::IDENTITY_STUDENT => '学生族',
    //     self::IDENTITY_EMPLOYEE => '上班族',
    //     self::IDENTITY_MOM => '宝妈',
    //     self::IDENTITY_UNEMPLOYED => '待业',
    //     self::IDENTITY_FREELANCER => '自由职业',
    // ]; // 宝妈
    
    
    
    protected $table = 'user'; // 待业
    protected $primaryKey = 'id'; // 自由职业
    protected $guarded = [];

    const MAN_AVATAR = 'https://oss.charityzimi.cn/head_pic/JXYmXzdqJze4CleCazRrdKNwo0P2M4CgNJFuNDsf.png';//默认男头像
    const GIRL_AVATAR = 'https://oss.charityzimi.cn/head_pic/uK5hvgxZBrGs9oa9HkKOKtUG096nFm3ZQYey5Ht2.png';//默认女头像
    /**
     * 教育经历
     * @return HasOne
     */
    public function education(): HasOne
    {
        return $this->hasOne(UserEducationModel::class, 'user_id');
    }

    /**
     * 工作经历
     * @return HasOne
     */
    public function workExperience(): HasMany
    {
        return $this->HasMany(UserWorkExperienceModel::class, 'user_id');
    }

    /**
     * 用户年龄
     * @return int
     */
    public function age(): int
    {
        if (!$this->birthday) return -1;
        return now()->parse($this->birthday)->age;
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier(): mixed
    {
        return $this->getKey();
    }

    // Rest omitted for brevity

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims(): array
    {
        return ['id'];
    }

    /**
     * 注销账号
     */
    public function unsubscribe()
    {
        $this->delete();
    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y/m/d H:i:s');
    }

    /**
     * 用户默认头像
     * @return mixed|string
     */
    public function defaultAvatar()
    {
        if ($this->avatar_url) return $this->avatar_url;
        if ($this->gender == self::GENDER_FEMALE) {
            return self::GIRL_AVATAR;
        }
        return self::MAN_AVATAR;
    }
}
